<?php
function sellerLog($log_info){
    $log_admin_id=empty(session('admin_id')) ? 0 : session('admin_id');
    // $seller = session('seller');
    $add['log_time'] = time();
    $add['log_seller_id'] = SELLER_ID;
    $add['log_admin_id'] = $log_admin_id;
    $add['log_seller_name'] = SELLER_NAME;
    $add['log_content'] = $log_info;
    $add['log_seller_ip'] = getIP();
    $add['log_store_id'] = STORE_ID;
    $add['log_url'] = __ACTION__;
    M('seller_log')->add($add);
}

function jsonReturn($status=0,$msg='',$data=''){
    if(empty($data))
        $data = '';
    $info['status'] = $status ? 1 : $status;
    $info['msg'] = $msg;
    $info['result'] = $data; 
    exit(json_encode($info));
}
function time2string($second){
	$day = floor($second/(3600*24));
	$second = $second%(3600*24);//除去整天之后剩余的时间
	$newday = ($day>0) ? floor((float)$day*24) : (float)$day;
	$hour = floor((float)$second/3600);
	$second = $second%3600;//除去整小时之后剩余的时间 
	$minute = floor($second/60);
	//返回字符串
	return $newday+$hour.'小时'.$minute.'分';
}

function cart_price($goods_id=0){ //更新购物车
			$goods=M('cart')->where('goods_id='.$goods_id)->select();//更新购物车
			
			foreach($goods as $v){
			$new_price=get_goods_huodong($v['goods_id'],$v['spec_key']);
				if($new_price){
					M('cart')->where('goods_id='.$v['goods_id'].' and `spec_key` = "'.$v['spec_key'].'"')->setField([
					'spec_key_name'=>$new_price["key_name"],
					'goods_name'=>$new_price["goods_name"],
					'goods_price'=>sanwei($new_price['price']),
					'member_goods_price'=>sanwei($new_price['price'])
					]);
				}else{
					M('cart')->where('goods_id='.$v['goods_id'].' and `spec_key` = "'.$v['spec_key'].'"')->delete();
				}
			}
}
function get_order_sn()
{
    // 保证不会有重复订单号存在
    while(true){
        $order_sn = date('YmdHis').rand(1000,9999); // 订单编号
        $order_sn_count = M('order')->where("order_sn = '$order_sn' or master_order_sn = '$order_sn'")->count();
        if($order_sn_count == 0)
            break;
    }
    return $order_sn;
}

function time3string($second){
    $day = floor($second/(3600*24));
    $second = $second%(3600*24);//除去整天之后剩余的时间
    $hour = floor($second/3600);
    $second = $second%3600;//除去整小时之后剩余的时间
    $minute = floor($second/60);
    $second = $second%60;//除去整分钟之后剩余的时间
    //返回字串
    return $day.'天'.$hour.'小时'.$minute.'分';
}

/**
 * 根据id获取地区名字
 * @param $regionId id
 */
function getRegionName($regionId){
    $data = M('region')->where(array('id'=>$regionId))->field('name')->find();
    return $data['name'];
}


/**
 * 生成h5和App的订单二维码(直接扫码后跳转酷点接口下单并付款)
 * @param $order
 * @param string $self
 * @param int $echo
 * @return string
 */
function appOrderQRcode($master_order_sn,$echo=0){
    // echo json_encode($order);exit;
    Vendor('phpqrcode.phpqrcode');
    $object = new \QRcode();
    if($master_order_sn){
        $mydata = SITE_URL.'/index.php/WXAPI/driver/payQrCode?master_order_sn='.$master_order_sn.'&is_qrcode=1';
        // $picname=\md5($mydata);
        $level = 'L'; //容错级别 分为L、M、Q、H
        $size = 4; //点的大小 1~10,手机端用4即可
        $filename = '/Public/appordersqrcode/'.$master_order_sn.'.png';
        if (!file_exists($filename)) {
            $object->png($mydata,'.'.$filename,$level,$size);
        }
    }

    if($echo){
        $img = file_get_contents(SITE_URL.$filename);
        $img = $img ?: file_get_contents('http://'.$_SERVER['HTTP_HOST'].$filename);
        $data = base64_encode($img);
        return 'data:image/png;base64,'.$data;
    }
}